package com.incar.chuxing.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.incar.chuxing.R;
import com.incar.chuxing.bean.MyCouponBean;
import com.incar.chuxing.utils.TimeUtils;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by Administrator on 2016/8/27.
 */
public class CouponsAdapter extends RecyclerView.Adapter<CouponsAdapter.ViewHolder> {
    private int flag;
    private LayoutInflater mInflater;
    private List<MyCouponBean.AttachmentBean.ModelsBean> mlist;
    private Context mContext;

    public CouponsAdapter(Context context, List<MyCouponBean.AttachmentBean.ModelsBean> list, int tabIndex) {
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
        mlist = list;
        this.flag = tabIndex;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView coupon_left, coupon_iv_ysy, coupon_iv_ygq;
        TextView coupon_price, coupon_content, coupon_title, coupon_fromData, coupon_endData;

        public ViewHolder(View itemView) {
            super(itemView);
        }
    }

    @Override
    public int getItemCount() {
        return mlist != null ? mlist.size() : 0;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_my_coupon, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.coupon_left = (ImageView) view.findViewById(R.id.coupon_left);
        viewHolder.coupon_iv_ysy = (ImageView) view.findViewById(R.id.coupon_iv_ysy);
        viewHolder.coupon_iv_ygq = (ImageView) view.findViewById(R.id.coupon_iv_ygq);
        viewHolder.coupon_price = (TextView) view.findViewById(R.id.coupon_price);
        viewHolder.coupon_content = (TextView) view.findViewById(R.id.coupon_content);
        viewHolder.coupon_title = (TextView) view.findViewById(R.id.coupon_title);
        viewHolder.coupon_fromData = (TextView) view.findViewById(R.id.coupon_fromData);
        viewHolder.coupon_endData = (TextView) view.findViewById(R.id.coupon_endData);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        DecimalFormat df = new DecimalFormat("0");
        holder.coupon_price.setText(df.format(mlist.get(position).getDenomination()) + "");
        holder.coupon_content.setText(mlist.get(position).getContent());
        holder.coupon_title.setText(mlist.get(position).getTitle());
        holder.coupon_fromData.setText(TimeUtils.getFormatDate(mlist.get(position).getStartDate() + "", "yyyy.MM.dd"));
        holder.coupon_endData.setText("—" + TimeUtils.getFormatDate(mlist.get(position).getEndDate() + "", "yyyy.MM.dd"));

        if (flag == 10) {
            holder.coupon_left.setImageResource(R.drawable.coupon_car);
        } else if (flag == 30) {
            holder.coupon_iv_ysy.setVisibility(View.VISIBLE);
            holder.coupon_left.setImageResource(R.drawable.coupon_car_no);
        } else if (flag == 18) {
            holder.coupon_iv_ygq.setVisibility(View.VISIBLE);
            holder.coupon_left.setImageResource(R.drawable.coupon_car_no);
        }
    }


}
